<?php
session_start ();
require_once '../tools/SqlHelper.class.php';
// 该类是一个业务逻辑处理类,主要完成对admin表操作
class AdminService {
	// 验证用户是否存在
	public function chekcIsHaveAdimn($id) {
		$sql = "select user_id from `user_tb` where `user_id`='$id'";
		// 创建一个SqlHelper对象
		$sqlHelper = new SqlHelper ();
		$res = $sqlHelper->execute_dql ( $sql );
		$row = mysql_num_rows ( $res );
		if ($row > 0) {
			// 用户已经存在
			return false;
		}
		// 释放资源
		// mysql_free_result ( $res );
		// 关闭连接
		$sqlHelper->close_connect ();
		return true;
	}
	// 添加新用户
	public function addAdimn($phoneNum, $password, $nickname, $sex, $birthday) {
		$sql = "INSERT INTO `user_tb` (`user_id`, `user_password`, `user_name`) VALUES ('$phoneNum', md5('$password'), '$nickname')";
		$sqlHelper = new SqlHelper ();
		$result = $sqlHelper->execute_dql ( $sql );
		$isAdd = true;
		if (! $result) {
			$isAdd = false; // 注册失败
		}
		$now_data = getdate ();
		$birthday = ($now_data ["year"] - ( int ) $birthday);
		$sql = "INSERT INTO `personal_data_tb` (`user_id`, `ps_birthday_year`,`ps_sex`) VALUES ('$phoneNum', '$birthday', '$sex')";
		$result = $sqlHelper->execute_dql ( $sql );
		if (! $result) {
			$isAdd = false; // 注册失败
		}
		$sql = "INSERT INTO `detail_data_tb` (`user_id`) VALUES ('$phoneNum')";
		$result = $sqlHelper->execute_dql ( $sql );
		if (! $result) {
			$isAdd = false; // 注册失败
		}
		$sql = "INSERT INTO `personals_condition_tb` (`user_id`) VALUES ('$phoneNum')";
		$result = $sqlHelper->execute_dql ( $sql );
		if (! $result) {
			$isAdd = false; // 注册失败
		}
		// 资源
		// mysql_free_result ( $result );
		$sqlHelper->close_connect ();
		return $isAdd;
	}
	
	// 提供一个验证用户是否合法 的方法
	public function chekcAdimn($id, $password) {
		$sql = "select * from `user_tb` where `user_id`='$id' and `user_password`=md5('$password')";
		// 创建一个SqlHelper对象
		$sqlHelper = new SqlHelper ();
		$result = $sqlHelper->execute_dql ( $sql );
		$row = mysql_fetch_array ( $result );
		if (! $row) {
			$sql = "select * from `admin_tb` where `ad_id`='$id' and `ad_password`=md5('$password')";
			$result = $sqlHelper->execute_dql ( $sql );
			$row = mysql_fetch_array ( $result );
			if (! $row) {
				$sqlHelper->close_connect ();
				return "1"; // 登录失败
			} else {
				$_SESSION ['userinfo'] = array (
						'userid' => $row ['ad_id'] 
				);
				$sqlHelper->close_connect ();
				return "2"; // 管理员账户登录
			}
		} else {
			// 否则将该用户信息保存到session中
			$_SESSION ['userinfo'] = array (
					'userid' => $row ['user_id'],
					'username' => $row ['user_name'] 
			);
			$sqlHelper->close_connect ();
			return "3"; // 会员登陆
		}
	}
	// 修改会员
	public function chageAdimn($user_tb_id, $phoneNum, $password, $nickname, $sex, $birthday) {
		$sqlHelper = new SqlHelper ();
		$sql = "UPDATE `user_tb` SET `user_id`='$phoneNum', `user_password`=md5('$password'), `user_name`='$nickname' WHERE `user_tb_id`=$user_tb_id";
		$result = $sqlHelper->execute_dql ( $sql );
		$isAdd = true;
		if (! $result) {
			$isAdd = false; // 修改失败
		}
		$now_data = getdate ();
		$birthday = ($now_data ["year"] - ( int ) $birthday);
		if ($sex == "男") {
			$sex = "0";
		} else {
			$sex = "1";
		}
		$sql = "UPDATE `personal_data_tb` SET `ps_birthday_year`='$birthday',`ps_sex`='$sex' WHERE `user_id`='$phoneNum'";
		$result = $sqlHelper->execute_dql ( $sql );
		if (! $result) {
			$isAdd = false; // 修改失败
		}
		// 资源
		// mysql_free_result ( $result );
		$sqlHelper->close_connect ();
		return $isAdd;
	}
}

?>